package org.csu.mypetstore_ssm.service;

import org.csu.mypetstore_ssm.domain.Account;
import org.csu.mypetstore_ssm.domain.Consignee;
import org.csu.mypetstore_ssm.persistence.AccountMapper;
import org.csu.mypetstore_ssm.persistence.ConsigneeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AccountService {

    @Autowired
    private AccountMapper accountMapper;

    @Autowired
    public ConsigneeMapper consigneeMapper = new ConsigneeMapper() {
        @Override
        public List<Consignee> getConsigneeByUsername(String username) {
            return null;
        }
    };

    public Account getAccountByUsername(String username) {
        return this.accountMapper.getAccountByUsername(username);
    }

    public Account getAccountByUsernameAndPassword(String username, String password) {
        Account account=new Account();
        account.setUsername(username);
        account.setPassword(password);
        return accountMapper.getAccountByUsernameAndPassword(account);
    }

    public void insertAccount(Account account) {
        this.accountMapper.insertAccount(account);
        this.accountMapper.insertProfile(account);
        this.accountMapper.insertSignon(account);
    }

    public void updateAccount(Account account) {
        this.accountMapper.updateAccount(account);
        this.accountMapper.updateProfile(account);
        if (account.getPassword() != null && account.getPassword().length() > 0) {
            this.accountMapper.updateSignon(account);
        }
    }

    public List<Consignee> getConsignee(String username){
        return this.consigneeMapper.getConsigneeByUsername(username);
    }


}
